(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:HuodongCtrl
     * @description
     * # HuodongCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('HuodongCtrl', HuodongCtrl);

    HuodongCtrl.$inject = ['$scope', 'dataService', 'helper', '$timeout', '$interval', '$rootScope', '$state', 'cacheService', '$location', '$sce'];

    function HuodongCtrl($scope, dataService, helper, $timeout, $interval, $rootScope, $state, cacheService, $location, $sce) {
        $scope.indexAction = function() {
            if ($rootScope) {
                //加载数据
                $scope.h5hd_id = $location.search().h5hd_id;
                $scope.h5hd = {};
                if ($scope.h5hd_id) {
                    $scope.getone();
                } else {
                    $.alert("非法参数");
                    // window.history.go(-5);
                }
            }
//             console.error("currentUser.", $rootScope);
        }

        $scope.getone = function() {
            // $scope.phone = 16172239821;
            dataService.h5hdGetOne($scope.h5hd_id).then(function(result) {
                if (result) {
                    $scope.h5hd = result;
                    $scope.h5hd.content1 = $sce.trustAsHtml($scope.h5hd.content1);
                    $scope.h5hd.content = $sce.trustAsHtml($scope.h5hd.content);
                    $timeout(function() {
                        document.title = result.title;
                    });
                    $scope.getdetailone();
                } else {
                    $.alert("没有发现活动！");
                }
            });
        }

        $scope.h5hddetlAdd = function() {
            if (!$scope.phone || String($scope.phone).length != 11) {
                $.alert("手机号不正确！");
                return;
            }
            var query = {
                content: {
                    h5hd_id: $scope.h5hd_id,
                    store_id: $scope.h5hd.store_id,
                    phone: $scope.phone,
                }
            };

            dataService.h5hddetlAdd(query).then(function(result) {
                if (!result.Msg) {
                    $.alert("领取成功！");
                    $scope.getdetailone();
                } else {
                    $.alert(result.Msg);
                }

            });
        }

        $scope.getdetailone = function() {

            var query = {
                content: {
                    h5hd_id: $scope.h5hd_id,
                    member_id: 1,
                }
            };

            return dataService.h5hddetailOne(query).then(function(result) {
                if (result) {
                    $scope.h5hddetail = result;
                }

                $rootScope.done();
            });
        }

    }
})();